﻿function Olemas() { }

Olemas.MaxPage = 0; // Total number of pages 
Olemas.NextPage = 1; // Next page to load
Olemas.FacultyId = 0; // The currently selected faculty
Olemas.ActionLink = ""; // The action that a method has to run
Olemas.DivToUpdate = ""; // The div to update with ajax
Olemas.Loading = false; // Is a page currently loading or not.
Olemas.CommentItemId = 0; // The id of the item the comment is for.
Olemas.CommentItemTypeId = 0; // The type of the item the comment is for (book=1, conspect=2, test=3)
Olemas.Page = ""; // The current page name
Olemas.Ordering = ""; // What to order the items by (title, author, etc.)

Olemas.HighlightFaculty = function () {
    /// <summary>
    /// Highlights faculty if it is selected
    /// </summary>
    if (Olemas.FacultyId != 0) {
        $("#fac" + Olemas.FacultyId).removeClass("sidebarItem").addClass("sidebarItemActive");
    }
};

Olemas.HighlightFilter = function ($item) {
    /// <summary>
    /// Highlights selected filter.
    /// </summary>
    $('.filterItems a').removeClass('filterSelected').addClass('filterItem');
    $item.attr("class", "filterSelected");
};

Olemas.OrderItems = function () {
    /// <summary>
    /// Orders items based on the ordering variable.
    /// </summary>
    Olemas.OrderChange();
    $.ajax({
        url: Olemas.ActionLink,
        data: { "page": Olemas.NextPage - 1, "facId": Olemas.FacultyId, "ordering": Olemas.Ordering },
        success: function (data) {
            $(Olemas.DivToUpdate).empty().html(data);
            Olemas.CheckPages();
            Olemas.OrderChange();
        }
    });
};

Olemas.OrderChange = function () {
    /// <summary>
    /// Hides filter links and content when order change is in progress.
    /// </summary>
    $(".filterItems label").toggle('fast');
    $(".pacmanLoadingImage").toggle();
    $(Olemas.DivToUpdate).toggle('fast');
};

Olemas.LoadMoreItems = function () {
    /// <summary>
    /// Loads more items to the given div
    /// </summary>
    $('.loadNextItemsDiv').remove();
    $(".loadingImage").show();
    $(window).unbind('scroll'); // remove scroll event.
    $.ajax({
        url: Olemas.ActionLink,
        data: { "page": Olemas.NextPage, "facId": Olemas.FacultyId, "ordering": Olemas.Ordering },
        success: function (data) {
            $(".loadingImage").hide();
            $(data).appendTo(Olemas.DivToUpdate);
            Olemas.NextPage++;
            Olemas.Loading = false;
            // rebind scroll event.
            $(window).scroll(
                    $.throttle(800, Olemas.ScrollLoad));
            Olemas.CheckPages();
        }
    });
    return false;

};

Olemas.LoadMoreComments = function () {
    /// <summary>
    /// Loads more comments to the given div
    /// </summary>
    $('#loadNextCommentsDiv').remove();
    $(".loadingImage").show();
    $(window).unbind('scroll'); // remove scroll event.
    $.ajax({
        url: Olemas.ActionLink,
        data: { "page": Olemas.NextPage, "itemId": Olemas.CommentItemId, "typeId": Olemas.CommentItemTypeId },
        success: function (data) {
            $(".loadingImage").hide();
            $(data).appendTo(Olemas.DivToUpdate);
            Olemas.NextPage++;
            Olemas.Loading = false;
            // rebind scroll event.
            $(window).scroll(
                    $.throttle(800, Olemas.ScrollLoad));
            Olemas.CheckPages();
        }
    });
    return false;
};

Olemas.ScrollLoad = function () {
    /// <summary>
    /// Loads more items when scrolled to the bottom.
    /// </summary>
    if (Olemas.Loading == true) return;
    if ($(window).scrollTop() >= ($(document).height() - $(window).height())) {
        if (Olemas.NextPage <= Olemas.MaxPage) {
            Olemas.Loading = true;
            if (Olemas.Page == "comments") {
                Olemas.LoadMoreComments();
            } else {
                Olemas.LoadMoreItems();
            }
        }

    }
};


Olemas.AddComment = function (method, action, commentData) {
    /// <summary>
    /// Sends comment to the add controller.
    /// </summary>
    Olemas.PrepareComments();
    $.ajax({
        type: method,
        url: action,
        data: commentData,
        success: function (data) {
            $(Olemas.DivToUpdate).empty().html(data); // IE fix
            Olemas.CommentSuccessNoty();
            Olemas.AfterComment();
            $("#commentTextBox").val("");
            $("#commentAuthorTextBox").val("");
            Olemas.NextPage = 2; // Reset the nextPage after comment is posted
            Olemas.CheckPages();
        },
        error: function () {
            Olemas.AfterComment();
            Olemas.CommentFailedNoty();
        }
    });
};

Olemas.CheckPages = function () {
    /// <summary>
    /// Shows the pager when there are pages to display.
    /// </summary>
    if (Olemas.NextPage <= Olemas.MaxPage || Olemas.NextPage -1 == Olemas.MaxPage) {
        if (Olemas.MaxPage == 1) {
            $('.loadNextItemsDiv').hide();
            $('#loadNextCommentsDiv').hide();
        } else {
            $('.loadNextItemsDiv').show();
            $('#loadNextCommentsDiv').show();
        }
    }
};

Olemas.PrepareComments = function() {
    /// <summary>
    /// "Lisa" button pressed.
    /// </summary>
    $("#loadNextCommentsDiv").hide();
    $("#commentSubmitButton").toggle(); // Disable submit button
    $('#commentsLoadingImage').toggle(); // Show loading image
};

Olemas.AfterComment = function() {
    /// <summary>
    /// Adding comment has failed or succeeded
    /// </summary>
    $('#commentsLoadingImage').toggle(); // Hide loading image.
    $("#commentSubmitButton").fadeIn(500); // Enable submit button.

};

Olemas.GetCommentData = function (form) {
    /// <summary>
    /// Creates comment object from form values.
    /// </summary>
    var commentData = {
        DateWritten: dateFormat(new Date(), 'isoUtcDateTime'), // todo: aeg on serveris vale.
        CommentText: form[1].value,
        UserName: form[0].value,
        ItemId: form[3].value,
        ItemType: form[2].value
    };
    return commentData;
};

Olemas.CommentSuccessNoty = function() {
    noty({
        text: 'Kommentaar lisatud!',
        type: "success",
        layout: "bottom",
        animation: {
            open: { height: 'toggle' },
            close: { height: 'toggle' },
            easing: 'swing',
            speed: 500
        },
        timeout: 2000
    });
};
Olemas.CommentFailedNoty = function() {
    noty({
        text: 'Kommentaari lisamine ebaõnnestus. Proovi uuesti.',
        type: "error",
        layout: "bottom",
        animation: {
            open: { height: 'toggle' },
            close: { height: 'toggle' },
            easing: 'swing',
            speed: 500
        },
        timeout: 2000
    });
};